#include "GrabbingEnemy.h"
#include "Player.h"
#include "Game.h"
#include "Animation.h"

#include <cmath>
GrabbingEnemy::GrabbingEnemy(SpawnInfo spawnInfo) : Enemy(spawnInfo)
{
    walking = new Animation("images/grabber/walking.bmp", 4, 100, COL_KEY);
    walking->setReferencePoint(walking->getWidth()/2, walking->getHeight());

	dead = new Image("images/grabber/dead.bmp", COL_KEY);
	dead->setReferencePoint(dead->getWidth()/2, dead->getHeight() - 5);
	//dead->setRefCentre();

	//grab = new Animation("images/player/punching.bmp", 3, 100, COL_KEY);
	grab = new Image("images/grabber/grabbing.bmp", COL_KEY);
	grab->setRefBottomCentre();

	fallingUp = new Image("images/grabber/falling-up.bmp", COL_KEY);
	fallingUp->setRefBottomCentre();

	fallingDown = new Image("images/grabber/falling-down.bmp", COL_KEY);
	fallingDown->setRefBottomCentre();

	//grab->setLooping(false);

    y = 0;
	x = 400;

	walkingSpeed = 4;
	grabbing = false;
}

GrabbingEnemy::~GrabbingEnemy()
{

}

void GrabbingEnemy::tick()
{
	if(!alive || u != 0) return;
	
	Player * p = Game::getInstance()->getPlayer();

	if(abs(p->getX() - x) > 18) grabbing = false;

	if(grabbing)
	{
		p->damage(GRABBER_DAMAGE, this, false);
		return;
	}


	if( advance(18) )
	{
		p->addGrabbingEnemy(this);
		//grab->start();
		grabbing = true;
	}
}

void GrabbingEnemy::handleDeath()
{
	Game::getInstance()->getPlayer()->removeGrabbingEnemy(this);
}
